import 'package:flutter/material.dart';
import 'package:flutter_common_ui/flutter_common_ui.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

OverlayEntry? overlayEntry;

void showDragPanelWidget(BuildContext context) {
  overlayEntry ??= OverlayEntry(
    builder: (context) {
      return DragPanelWidget(
        alignment: Alignment.bottomLeft,
        isRight: true,
        isTop: true,
        defaultHeight: 300,
        child: Text("DDDDDD"),
      );
    },
  );
  if (overlayEntry!.mounted) {
    overlayEntry!.remove();
  }
  Overlay.of(context).insert(overlayEntry!);
}

void hideDragPanelWidget() {
  overlayEntry?.removeX();
  overlayEntry = null;
}

//使用者
OverlayEntry? _overlayEntryFloating;

void floatingDragButtonShow(BuildContext context, FloatingDragButton widget) {
  _overlayEntryFloating ??= OverlayEntryX(
    builder: (context) {
      return widget;
    },
  );
  if (_overlayEntryFloating!.mounted) {
    _overlayEntryFloating!.remove();
  }
  Overlay.of(context).insert(_overlayEntryFloating!);
}

void floatingDragButtonHide() {
  _overlayEntryFloating?.remove();
}

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  final rangeValues = RangeValues(0, 100).obs;
  final sliderValue = 50.0.obs;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text("My Page")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FilledButton(
              onPressed: () {
                floatingDragButtonShow(
                    context,
                    FloatingDragButton(
                      buttonWidth: 100,
                      buttonHeight: 40,
                    ));
              },
              child: Text(
                "Test Button1",
              ),
            ),
            FilledButton(
              onPressed: () {
                hideDragPanelWidget();
                showDragPanelWidget(context);
                // SmartDialog.showToast("dddddd", alignment: Alignment.center);
                // //延迟500
                // Future.delayed(Duration(milliseconds: 500), () {
                //   SmartDialog.showLoading();
                // });
                //延迟500
                // Future.delayed(Duration(milliseconds: 1500), () {
                //   SmartDialog.showLoading(msg: "加载中...", clickMaskDismiss: true);
                // });
              },
              child: Text(
                "showDragPanelWidget",
              ),
            ),
            RotateWidget(
              angle: -1,
              endAngle: 1,
              animate: true,
              repeat: true,
              reverse: true,
              child: SizedBox(
                width: 100,
                height: 100,
                child: Container(
                  color: Colors.red,
                  child: Text("dddd"),
                ),
              ),
            ),
            ScaleWidget(
              scale: 0.5,
              animate: true,
              repeat: true,
              reverse: true,
              child: SizedBox(
                width: 100,
                height: 100,
                child: Container(
                  color: Colors.red,
                  child: Text("dddd"),
                ),
              ),
            ),


            Obx(
              () => SliderWidget(
                value: sliderValue.value,
                min: 0,
                max: 100,
                bubbleActive: true,
                trackHeight: 10,
                thumbRadius: 12,
                // divisions: 10,
                height: 90,
                onChanged: (it) {
                  sliderValue.value = it;
                },
              ),
            ),
            Obx(
              () => RangeSliderWidget(
                values: rangeValues.value,
                min: 0,
                max: 100,
                bubbleActive: true,

                thumbRadius: 10,
                onChanged: (it) {
                  rangeValues.value = it;
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
